package com.markan.facebook.service;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.markan.facebook.Facebook;
import com.markan.facebook.education.service.ConcentrationVO;
import com.markan.facebook.education.service.EducationVO;
import com.markan.facebook.education.service.SchoolVO;
import com.markan.facebook.group.service.GroupVO;
import com.markan.facebook.language.service.LanguageVO;
import com.markan.facebook.location.service.LocationVO;
import com.markan.facebook.project.service.ProjectVO;
import com.markan.facebook.user.service.UserVO;
import com.markan.facebook.utl.MknJSONUtil;
import com.markan.facebook.work.service.EmployerVO;
import com.markan.facebook.work.service.PositionVO;
import com.markan.facebook.work.service.WorkVO;

public class FacebookService extends Facebook {

	public FacebookService(String access_token) throws Exception {
		super(access_token);
	}
	
	/**
	 * 유저 정보를 UserVO 로 반환한다.
	 * @param jsonObj
	 * @return
	 * @throws Exception
	 */
	public UserVO getUserDetail(String user_id) throws Exception{
		
		//User 정보를 가져온다.
		JSONObject jsonObj = getUserInfo(user_id);
		
		UserVO user = new UserVO();
		
		// 사용자 기본정보
		user.setId((String) jsonObj.get("id")); 					//사용자 ID
		user.setName((String) jsonObj.get("name"));					//사용자 풀네임
		user.setFirstName((String) jsonObj.get("first_name"));		//사용자 이름
		user.setMiddleName((String) jsonObj.get("middle_name"));	//사용자 중간 이름
		user.setLastName((String) jsonObj.get("last_name"));		//사용자 성
		user.setLink((String) jsonObj.get("link"));					//사용자 링크 주소
		user.setUsername((String) jsonObj.get("username"));			//사용자 Bio
		user.setBio((String) jsonObj.get("bio"));	
		user.setBirthday((String) jsonObj.get("birthday"));
		
		user.setGender((String) jsonObj.get("gender"));
		user.setTimezone( MknJSONUtil.getObject2Integer(jsonObj.get("timezone")) );
		user.setVerified( MknJSONUtil.getObject2Booelan(jsonObj.get("verified")) );
		user.setUpdated_time((String) jsonObj.get("updated_time"));
		
		//hometown
		JSONObject hometown = MknJSONUtil.getObject2JSONObject( jsonObj.get("hometown"));
		user.setHometown(new LocationVO((String) hometown.get("id"), (String) hometown.get("name")));
		
		//location
		JSONObject location  = MknJSONUtil.getObject2JSONObject( jsonObj.get("location"));
		user.setHometown(new LocationVO((String) location.get("id"), (String) location.get("name")));
		
		//-----------
		//languages
		//-----------
		JSONArray languagesArray = MknJSONUtil.getObject2JSONArray( jsonObj.get("languages"));
		List<LanguageVO> languageList = new ArrayList<LanguageVO>();
		for(int i=0, languagesSize = languagesArray.size(); i<languagesSize; i++ ) {
			JSONObject languages = (JSONObject) languagesArray.get(i);
			languageList.add(new LanguageVO((String) languages.get("id"), (String) languages.get("name")));
		}
		user.setLanguageList(languageList);
		
		//==========================================
		// work start
		//==========================================
		List<WorkVO> workList = new ArrayList<WorkVO>();	//경력 목록
		JSONArray workArray = MknJSONUtil.getObject2JSONArray( jsonObj.get("work"));
		for(int i=0, workSize = workArray.size(); i<workSize; i++ ) {
			JSONObject workJSON = (JSONObject) workArray.get(i);
			WorkVO work = new WorkVO();
			
			//employer(회사)
			JSONObject work_employer = MknJSONUtil.getObject2JSONObject( workJSON.get("employer"));
			work.setEmployer(new EmployerVO((String) work_employer.get("id"), (String) work_employer.get("name")));
			
			//position(직급)
			JSONObject work_position = MknJSONUtil.getObject2JSONObject( workJSON.get("position"));
			work.setPosition(new PositionVO((String) work_position.get("id"), (String) work_position.get("name")));
			
			//시작일
			work.setStart_date((String) workJSON.get("start_date"));
			
			//projects(프로젝트)
			List<ProjectVO> projectList = new ArrayList<ProjectVO>();	//프로젝트 목록
			JSONArray projectsArray = MknJSONUtil.getObject2JSONArray( workJSON.get("projects"));
			for(int j=0, projectsSize = projectsArray.size(); j<projectsSize; j++ ) {
				JSONObject projectJSON = (JSONObject) projectsArray.get(j);
				
				ProjectVO project = new ProjectVO();
				project.setId((String) projectJSON.get("id"));		//프로젝트 ID
				project.setName((String) projectJSON.get("name"));	//프로젝트명
				
				//with(참여자 목록)
				List<UserVO> withList = new ArrayList<UserVO>();
				JSONArray projectsWithArray = MknJSONUtil.getObject2JSONArray( projectJSON.get("with"));
				for(int k=0, projectsWithSize = projectsWithArray.size(); k<projectsWithSize; k++ ) {
					JSONObject project_with = (JSONObject) projectsWithArray.get(k);
					withList.add( new UserVO((String) project_with.get("id"), (String) project_with.get("id")) );
				}
				project.setWithList(withList);
				
				//from(프로젝트 작성자)
				JSONObject project_from = projectJSON.getJSONObject("from");
				project.setFrom( new UserVO((String) project_from.get("id"), (String) project_from.get("id")) );
				
				projectList.add(project);
			}
			
			workList.add(work);
		}
		user.setWorkList(workList);
		// ==========================================
		
		//-----------
		//education
		//-----------
		List<EducationVO> educationList = new ArrayList<EducationVO>();
		JSONArray educationArray = MknJSONUtil.getObject2JSONArray( jsonObj.get("education"));
		for(int i=0, educationSize = educationArray.size(); i<educationSize; i++ ) {
			JSONObject education = (JSONObject) educationArray.get(i);
			
			EducationVO educationVO = new EducationVO();
			educationVO.setEducationType((String) education.get("type"));
			
			//school
			JSONObject education_school = MknJSONUtil.getObject2JSONObject( jsonObj.get("school"));
			educationVO.setSchool(new SchoolVO( (String) education_school.get("id"), (String) education_school.get("name") ));
			
			//concentration(학과)
			List<ConcentrationVO> concentrationList = new ArrayList<ConcentrationVO>();
			JSONArray concentrationArray = MknJSONUtil.getObject2JSONArray( education.get("concentration"));
			for(int j=0, concentrationSize = concentrationArray.size(); j<concentrationSize; j++ ) {
				JSONObject concentration = (JSONObject) concentrationArray.get(j);
				concentrationList.add( new ConcentrationVO((String) concentration.get("id"), (String) concentration.get("name")) );	
			}
			educationVO.setConcentrationList(concentrationList);
			educationList.add(educationVO);
		}
		user.setEducationList(educationList);
		
		return user;
	}
	public UserVO getMyDeatil() throws Exception{
		return getUserDetail("me/");
	}
	
	/**
	 * 그룹 목록을 가져온다.
	 * @return
	 * @throws Exception
	 */
	public List<GroupVO> getGroupList() throws Exception{
		//그룹 정보를 가져온다.
		JSONObject jsonObj = getMyGroupInfo();
		
		List<GroupVO> groupList = new ArrayList<GroupVO>();
		JSONArray groupsJSONArr = MknJSONUtil.getObject2JSONArray( jsonObj.get("data"));
		for(int i=0, groupSize = groupsJSONArr.size(); i<groupSize; i++ ) {
			JSONObject groupJsonObj = (JSONObject) groupsJSONArr.get(i);
			
			int version = MknJSONUtil.getObject2Integer(groupJsonObj.get("version"));
			String name = (String) groupJsonObj.get("name");
			String id = (String) groupJsonObj.get("id");
			boolean administrator = MknJSONUtil.getObject2Booelan(groupJsonObj.get("administrator"));
			int bookmarkOrder = MknJSONUtil.getObject2Integer(groupJsonObj.get("bookmark_order"));
			
			GroupVO group = new GroupVO();
			group.setVersion(version);
			group.setName(name);
			group.setId(id);
			group.setAdministrator(administrator);
			group.setBookmarkOrder(bookmarkOrder);
			
			groupList.add(group);
			
		}
		return groupList;
	}

}
